﻿using System;

public class Mapper023 : MapperDefault
{
    private int irq_counter;
    private int irq_enabled;
    private int irq_latch;
    internal int patch = 0xffff;
    private int[] regs = new int[9];

    public override void init(NES nes)
    {
        base.init(nes);
        this.reset();
    }

    public override void loadROM(ROM rom)
    {
        if (!rom.Valid)
        {
            Console.Out.WriteLine("VRC2: Invalid ROM! Unable to load.");
        }
        else
        {
            int num = rom.RomBankCount * 2;
            this.load8kRomBank(0, 0x8000);
            this.load8kRomBank(1, 0xa000);
            this.load8kRomBank(num - 2, 0xc000);
            this.load8kRomBank(num - 1, 0xe000);
            this.loadCHRROM();
            base.nes.Cpu.requestIrq(2);
        }
    }

    public override void reset()
    {
        this.regs[0] = 0;
        this.regs[1] = 1;
        this.regs[2] = 2;
        this.regs[3] = 3;
        this.regs[4] = 4;
        this.regs[5] = 5;
        this.regs[6] = 6;
        this.regs[7] = 7;
        this.regs[8] = 0;
        this.irq_enabled = 0;
        this.irq_latch = 0;
        this.irq_counter = 0;
    }

    public override int syncH(int scanline)
    {
        if ((this.irq_enabled & 2) != 0)
        {
            if (this.irq_counter == 0xff)
            {
                this.irq_counter = this.irq_latch;
                this.irq_enabled = (this.irq_enabled & 1) * 3;
                return 3;
            }
            this.irq_counter++;
        }
        return 0;
    }

    public override void write(int address, short value_Renamed)
    {
        if (address < 0x8000)
        {
            base.write(address, value_Renamed);
            return;
        }
        int num = address & this.patch;
        if (num <= 0xa008)
        {
            if (num > 0x800c)
            {
                if (num > 0x9008)
                {
                    switch (num)
                    {
                        case 0xa000:
                        case 0xa004:
                        case 0xa008:
                            goto Label_028B;
                    }
                    return;
                }
                if (num == 0x9000)
                {
                    if (value_Renamed != 0xff)
                    {
                        value_Renamed = (short) (value_Renamed & 3);
                        if (value_Renamed == 0)
                        {
                            base.nes.Ppu.Mirroring = 0;
                            return;
                        }
                        if (value_Renamed == 1)
                        {
                            base.nes.Ppu.Mirroring = 1;
                            return;
                        }
                        if (value_Renamed == 2)
                        {
                            base.nes.Ppu.Mirroring = 3;
                            return;
                        }
                        base.nes.Ppu.Mirroring = 4;
                    }
                    return;
                }
                if (num == 0x9008)
                {
                    this.regs[8] = value_Renamed & 2;
                }
                return;
            }
            if (num > 0x8004)
            {
                switch (num)
                {
                    case 0x8008:
                    case 0x800c:
                        goto Label_01F7;
                }
                return;
            }
            if ((num != 0x8000) && (num != 0x8004))
            {
                return;
            }
        }
        else
        {
            switch (num)
            {
                case 0xf004:
                    this.irq_latch = (this.irq_latch & 15) | ((value_Renamed & 15) << 4);
                    return;

                case 0xf008:
                    this.irq_enabled = value_Renamed & 3;
                    if ((this.irq_enabled & 2) != 0)
                    {
                        this.irq_counter = this.irq_latch;
                    }
                    return;

                case 0xf00c:
                    this.irq_enabled = (this.irq_enabled & 1) * 3;
                    return;

                case 0xe000:
                    this.regs[6] = (this.regs[6] & 240) | (value_Renamed & 15);
                    this.load1kVromBank(this.regs[6], 0x1800);
                    return;

                case 0xe001:
                case 0xe004:
                    this.regs[6] = (this.regs[6] & 15) | ((value_Renamed & 15) << 4);
                    this.load1kVromBank(this.regs[6], 0x1800);
                    return;

                case 0xe002:
                case 0xe008:
                    this.regs[7] = (this.regs[7] & 240) | (value_Renamed & 15);
                    this.load1kVromBank(this.regs[7], 0x1c00);
                    return;

                case 0xe003:
                case 0xe00c:
                    this.regs[7] = (this.regs[7] & 15) | ((value_Renamed & 15) << 4);
                    this.load1kVromBank(this.regs[7], 0x1c00);
                    return;

                case 0xe005:
                case 0xe006:
                case 0xe007:
                case 0xe009:
                case 0xe00a:
                case 0xe00b:
                case 0xc005:
                case 0xc006:
                case 0xc007:
                case 0xc009:
                case 0xc00a:
                case 0xc00b:
                case 0xd005:
                case 0xd006:
                case 0xd007:
                case 0xd009:
                case 0xd00a:
                case 0xd00b:
                case 0xb005:
                case 0xb006:
                case 0xb007:
                case 0xb009:
                case 0xb00a:
                case 0xb00b:
                    return;

                case 0xf000:
                    this.irq_latch = (this.irq_latch & 240) | (value_Renamed & 15);
                    return;

                case 0xc000:
                    this.regs[2] = (this.regs[2] & 240) | (value_Renamed & 15);
                    this.load1kVromBank(this.regs[2], 0x800);
                    return;

                case 0xc001:
                case 0xc004:
                    this.regs[2] = (this.regs[2] & 15) | ((value_Renamed & 15) << 4);
                    this.load1kVromBank(this.regs[2], 0x800);
                    return;

                case 0xc002:
                case 0xc008:
                    this.regs[3] = (this.regs[3] & 240) | (value_Renamed & 15);
                    this.load1kVromBank(this.regs[3], 0xc00);
                    return;

                case 0xc003:
                case 0xc00c:
                    this.regs[3] = (this.regs[3] & 15) | ((value_Renamed & 15) << 4);
                    this.load1kVromBank(this.regs[3], 0xc00);
                    return;

                case 0xd000:
                    this.regs[4] = (this.regs[4] & 240) | (value_Renamed & 15);
                    this.load1kVromBank(this.regs[4], 0x1000);
                    return;

                case 0xd001:
                case 0xd004:
                    this.regs[4] = (this.regs[4] & 15) | ((value_Renamed & 15) << 4);
                    this.load1kVromBank(this.regs[4], 0x1000);
                    return;

                case 0xd002:
                case 0xd008:
                    this.regs[5] = (this.regs[5] & 240) | (value_Renamed & 15);
                    this.load1kVromBank(this.regs[5], 0x1400);
                    return;

                case 0xd003:
                case 0xd00c:
                    this.regs[5] = (this.regs[5] & 15) | ((value_Renamed & 15) << 4);
                    this.load1kVromBank(this.regs[5], 0x1400);
                    return;

                case 0xb000:
                    this.regs[0] = (this.regs[0] & 240) | (value_Renamed & 15);
                    this.load1kVromBank(this.regs[0], 0);
                    return;

                case 0xb001:
                case 0xb004:
                    this.regs[0] = (this.regs[0] & 15) | ((value_Renamed & 15) << 4);
                    this.load1kVromBank(this.regs[0], 0);
                    return;

                case 0xb002:
                case 0xb008:
                    this.regs[1] = (this.regs[1] & 240) | (value_Renamed & 15);
                    this.load1kVromBank(this.regs[1], 0x400);
                    return;

                case 0xb003:
                case 0xb00c:
                    this.regs[1] = (this.regs[1] & 15) | ((value_Renamed & 15) << 4);
                    this.load1kVromBank(this.regs[1], 0x400);
                    return;

                case 0xa00c:
                    goto Label_028B;
            }
            return;
        }
    Label_01F7:
        if (this.regs[8] != 0)
        {
            this.load8kRomBank(value_Renamed, 0xc000);
            return;
        }
        this.load8kRomBank(value_Renamed, 0x8000);
        return;
    Label_028B:
        this.load8kRomBank(value_Renamed, 0xa000);
    }
}

